import {defineStore} from 'pinia'

const useCounterStore = defineStore('appStore',{
  state :()=> ({
    NavList: [],  //menu导航栏的数据
    TabsList:[],
    RoutersList :[], //保存登录获取到的路由信息
    NowRouterPath :'',
    TabDelete_ToPath:'',
    menus:'',//一级菜单选中
    menuTitleId:0,//当前选中菜单的上级id
  }),
  actions : {
     //每次点击顶部导航筛选数据 
      oneSetMenuList (ids:any){
        //保存用户点击顶部分类导航的id
        this.Top_Menu_id = ids
        let oneListNav = JSON.parse(sessionStorage.getItem('NavList'))
        oneListNav.map(item =>{
          if(item.id == ids){
            let list  = []
            list.push(item) 
            this.NavList = list
          }
        })
        //根据当前的路由从this.NavList中筛选出对应的id
        this.NavList.map(item=>{
          console.log('item',item);
          item.children.map(C_item =>{
            console.log('C_item',C_item,'this.NowRouterPath',this.NowRouterPath,'item.id',item.id);
            if(C_item.web_path == this.NowRouterPath){
              this.menuTitleId = item.id
            }
          })
        })
        
      },
     
      //点击tabs,
      getTitle(path:string){
        this.menus=path
      },
      //保存第一次登录获取到的路由信息
      oneSetRouterList(obj:any){
        this.RoutersList = obj
      },
     
    getNowRouterPath(path:object){
      console.log('路由传递的路径',path);
      this.NowRouterPath = path.fullPath
      console.log('刚刚传递过来this.NavList',this.NavList);
      
      this.screenTabsList(path)
      this.getTitle(path.fullPath)
    },
    //筛选tabs的数据显示
    screenTabsList(to_data:object){
      this.TabsList = JSON.parse(sessionStorage.getItem('TabsList'))
      if(this.TabsList == null){
        if(to_data.fullPath !== '/' ){
          this.TabsList= []
          this.TabsList.push(to_data)
        }
      }else {
        if(to_data.path != '/' && !this.TabsList.some(item => item.fullPath == to_data.fullPath)){
          this.TabsList.push(to_data)
        }
      }
      sessionStorage.setItem('TabsList',JSON.stringify(this.TabsList) )
    },
    //标签页删除
    DeleteTabsItem(path:string){
        this.TabsList.map((item,index) => {
          if(item.fullPath == path){
            this.TabsList.splice(index,1)
            this.TabDelete_ToPath = (this.TabsList[index] == undefined  ?  this.TabsList[this.TabsList.length -1].fullPath  : this.TabsList[index] .fullPath)
          }
        })
        sessionStorage.setItem('TabsList', JSON.stringify(this.TabsList) )
    },
    //点击退出登录清楚数据
    Click_logOf(){
      this.NavList  = []  
      this.TabsList = []
      this.NowRouterPath = ''
      this.TabDelete_ToPath = ''
    },
  },
})

export default useCounterStore